library(SDMTools)

work.dir = "H:/Barra Work Directory/bioclim/"; setwd(work.dir)

pnts=cbind(x=c(112,116,116,112), y=c(-11,-11,-18.5,-18.5))
cols = colorRampPalette(c('tan', 'lightblue', 'slateblue', 'blue', 'blue4', 'black'))(101)
labels=c('Annual Mean Temp', 'Mean Diurnal Range', 'Isothermality', 'Temp. Seasonality', 'Max. Temp. Warmest Period', 'Min. Temp. Coldest Period', 'Temp. Annual Range', 'Mean Temp. Wettest Quarter', 'Mean Temp. Driest Quarter', 'Mean Temp. Warmest Quarter', 'Mean Temp. Coldest Quarter', 'Annual Precipitation', 'Pr. Wettest Period', 'Pr. Driest Period', 'Precipitation Seasonality', 'Pr. Wettest Quarter', 'Pr. Driest Quarter', 'Pr. Warmest Quarter', 'Pr.Coldest Quarter')

zlim=NULL
for (ii in c(13,14)) { cat(ii,'\n')
      tasc = read.asc.gz(paste("bioclim_", sprintf('%02i',ii), ".asc.gz",sep=''))
      zlim=range(c(zlim,as.vector(tasc)),na.rm=TRUE)
}
zlim=c(0,800) #given what you know about zlim

png(paste('bioclimpr_period.png',sep=''), width=14, height=6.5, units='cm', res=300, pointsize=5, bg='white')


#make 4 columns of 3 rows of images 
par(mfrow=c(1,2),mar=c(0,1,0,1), oma=c(0,3,3,0))


#loop
for (ii in c(13,14)) {  cat(ii,'\n')
      tasc = read.asc.gz(paste("bioclim_", sprintf('%02i',ii), ".asc.gz",sep=''))
      image(tasc, zlim=zlim, ann=FALSE,axes=FALSE,col=cols)
      text (128, -43, labels[ii], cex=2)
      if (ii==13) {legend.gradient(pnts,cols=cols,limits=round(zlim), title='Precipitation', cex=2)}

      }

dev.off()
